<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>
        人员添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/statics/css/main.css" media="all">
    <script type="text/javascript" src="${pageContext.request.contextPath }/statics/lib/loading/okLoading.js"></script>
    <style>
        .imgs {
            display: none;
        }

        .picture {
            display: none;
        }
    </style>
</head>
<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>人员添加</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon"
                                                                        style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <form class="layui-form layui-form-pane" action="AddFileServlet" id="add" enctype="multipart/form-data"
          method="post">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <%--<div class="layui-tab-content">
            <div class="layui-tab-item layui-show">--%>
            <input type="hidden" name="id" value="${param.id}">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class='x-red'>*</span>所属房产
                </label>
                <div class="layui-input-block">
                    <input type="hidden" value="" name="home_id" id="home_id"/>
                    <select name="homeId" id="homeId" lay-filter="home">
                        <option value="0">---请选择所属房产---</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class='x-red'>*</span>成员名称
                </label>
                <div class="layui-input-block">
                    <input type="text" name="memberName" autocomplete="off" placeholder="输入成员名称"
                           class="layui-input" lay-reqtext="成员名称是必填项，岂能为空？" value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    身份证号
                </label>
                <div class="layui-input-block">
                    <input type="text" name="card" autocomplete="off" placeholder="请输入身份证号码"
                           class="layui-input" value=""> <%--maxlength="18" minlength="18" lay-verify="identity"--%>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    出生日期
                </label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="birthday" placeholder="出生日期" id="LAY_demorange_s" value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    联系方式
                </label>
                <div class="layui-input-block">
                    <input type="text" name="phone" autocomplete="off" placeholder=""
                           class="layui-input" maxlength="11" value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    职业
                </label>
                <div class="layui-input-block">
                    <input type="text" name="occupation" autocomplete="off" placeholder=""
                           class="layui-input" value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class='x-red'>*</span>性别
                </label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="1" title="男" checked="checked">
                    <div class="layui-unselect layui-form-radio layui-form-radioed"><i
                            class="layui-anim layui-icon layui-anim-scaleSpring"></i>
                        <div>男</div>
                    </div>
                    <input type="radio" name="sex" value="2" title="女">
                    <div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>
                        <div>女</div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class='x-red'>*</span>成员类型
                </label>
                <div class="layui-input-block">
                    <input type="radio" name="personnelType" value="1" title="业主" checked="checked">
                    <div class="layui-unselect layui-form-radio layui-form-radioed"><i
                            class="layui-anim layui-icon layui-anim-scaleSpring"></i>
                        <div>业主</div>
                    </div>
                    <input type="radio" name="personnelType" value="2" title="家庭成员">
                    <div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>
                        <div>家庭成员</div>
                    </div>
                    <input type="radio" name="personnelType" value="3" title="租户">
                    <div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>
                        <div>租户</div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red"></span>成员照片
                </label>
                <div class="layui-input-inline">
                    <div class="site-demo-upbar">
                        <div class=" layui-upload-button" style="border:#FFFFFF ;">
                            <button type="button" class="layui-btn" id="test1">
                                <i class="layui-icon">&#xe67c;</i>上传图片
                            </button>
                            <input class="layui-upload" type="file" accept="undefined" id="previewImg" name="images"
                                   onchange="upload(this)">
                        </div>
                    </div>
                </div>
                <%--<a href="javascript:;" style="" class="layui-btn " id="cancel"><i
                        class="layui-icon">ဂ</i>撤销上传</a>--%>
            </div>
            <div class="layui-form-item imgs" id="imgshow">
                <label class="layui-form-label">照片展示</label>
                <img src="" id="pimages" name="pimages" style="width: 400px;height: 200px;"/>
                <input id="avatar" name="photoPath" type="hidden" value="">
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">
                    描述
                </label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" name="remark" class="layui-textarea"></textarea>
                </div>
                <%--</div>
            </div>--%>
            </div>
            <div class="layui-form-item" align="center">
                <button class="layui-btn" type="button" lay-filter="add" lay-submit="">
                    保存
                </button>
                <button class="layui-btn" type="reset">
                    重置
                </button>
            </div>
        </div>
    </form>
</div>
<script src="${pageContext.request.contextPath }/statics/lib/layui/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/statics/js/x-layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/statics/js/jquery.min.js" charset="UTF-8"></script>
<script>
    if ($("input[name='id']").val() != "") {    //根据id查询数据
        var id = $("input[name='id']").val()
        $.get("/personnel/" + id, function (data) {
            console.log(data)
            $("input[name='home_id']").val(data.data.homeId);
            $("input[name='memberName']").val(data.data.memberName);
            $("input[name='card']").val(data.data.card);
            $("input[name='birthday']").val(data.data.birthday);
            $("input[name='phone']").val(data.data.phone);
            $("input[name='occupation']").val(data.data.occupation);
            if (data.data.sex == 2) {
                $("input[name='sex']").attr("checked", true)
            }
            if (data.data.personnelType == 2) {
                $("input[name='personnelType']").eq(1).attr("checked", true)
            }
            if (data.data.personnelType == 3) {
                $("input[name='personnelType']").eq(2).attr("checked", true)
            }
            if (data.data.photoPath != "" && data.data.photoPath != null) {
                $("#pimages").attr('src', data.data.photoPath);
                $("#avatar").val(data.data.photoPath);
                $(".imgs").show();
            }
            $("textarea[name='remark']").val(data.data.remark);
        })
    }

    layui.use(['laydate', 'element', 'layer', 'form'], function () {
        $ = layui.jquery;//jquery
        lement = layui.element();//面包导航
        layer = layui.layer;//弹出层
        form = layui.form();
        laydate = layui.laydate;//日期插件
        okLoading.close($);

        form.render();

        var start = {
            max: '2027-1-1'
            , choose: function (datas) {
            }
        };
        document.getElementById('LAY_demorange_s').onclick = function () {
            start.elem = this;
            laydate(start);
        };
        //监听提交
        form.on('submit(add)', function (data) {
            if ($("#homeId").val() == 0) {
                layer.msg('请选择所属房产', {icon: 5, time: 2000});
                return false;
            }
            if ($("input[name='memberName']").val() == "") {
                layer.msg('成员名称不能为空', {icon: 5, time: 2000});
                return false;
            }
            /*            if ($("input[name='card']").val() == "") {
                            layer.msg('身份证号不能为空', {icon: 5, time: 2000});
                            return false;
                        }
                        if ($("input[name='birthday']").val() == "") {
                            layer.msg('出生日期不能为空', {icon: 5, time: 2000});
                            return false;
                        }
                        if ($("input[name='phone']").val() == "") {
                            layer.msg('联系方式不能为空', {icon: 5, time: 2000});
                            return false;
                        }
                        if ($("input[name='occupation']").val() == "") {
                            layer.msg('职业不能为空', {icon: 5, time: 2000});
                            return false;
                        }*/
            /*if ($("input[name='imgs']").val() == "") {
                 layer.msg('请上传一张本人照片', {icon: 5, time: 2000});
                 return false;
             }*/
            var data = data.field;
            console.log(data)
            if ($("input[name='id']").val() != "") {
                $.ajax({
                    type: "put",
                    url: "update",
                    data: data,
                    dataType: "json",
                    success: function (data) {
                        if (data.data == true) {
                            layer.msg(data.msg, {icon: 6, time: 2000}, function () {
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                return false;
                            });
                        } else {
                            layer.msg("上传失败", {icon: 5, time: 2000});
                            return false;
                        }
                    }
                });
            }

            if ($("input[name='id']").val() == "") {    //增加
                $.ajax({
                    type: "post",
                    url: "add",
                    data: data,
                    dataType: "json",
                    success: function (data) {
                        if (data.data == true) {
                            layer.msg(data.msg, {icon: 6, time: 2000}, function () {
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                return false;
                            });
                        } else {
                            layer.msg("上传失败", {icon: 5, time: 2000});
                            return false;
                        }
                    }
                });
            }
        })

        //加载所属房产
        function homeload() {
            $.ajax({
                type: "GET",//请求类型
                url: "/home/getlist",//请求的ur
                dataType: "json",//ajax接口（请求url）返回的数据类型
                success: function (data) {//data：返回数据（json对象）
                    var home_id = $("#home_id").val()
                    var options = "<option value='0'>---请选择所属房产---</option>";
                    for (var i = 0; i < data.data.length; i++) {
                        if (home_id != null && home_id != undefined && data.data[i].id == home_id) {
                            options += "<option selected='selected' value=" + data.data[i].id + ">" + data.data[i].homeName + "</option>";
                        } else {
                            options += "<option value=" + data.data[i].id + ">" + data.data[i].homeName + "</option>";
                        }
                    }
                    $("#homeId").html(options);
                    form.render('select', 'homeId');
                },
                error: function (data) {//当访问时候，404，500 等非200的错误状态码
                    alert("加载栋数失败！");
                }
            });
        }

        homeload()
    })
</script>
<!--成员照片上传-->
<script>
    function upload(obj, id) {
        var formData = new FormData();
        formData.append('images', $('#previewImg')[0].files[0]);
        formData.append('id', 1);
        layer.msg('图片上传中', {icon: 16});
        $.ajax({
            type: "post",
            async: true,
            processData: false,
            contentType: false,
            url: "/upload/uploadFile",
            data: formData,
            success: function (data) {
                console.log(data)
                if (data.msg == '执行成功') {
                    layer.closeAll('loading');
                    $("#pimages").attr('src', data.data);
                    $("#avatar").val(data.data);
                    $(".imgs").show();
                    return false;
                } else {
                    layer.msg("操作失败", {icon: 2, time: 1000});
                }
            }
        });
    }
</script>
</body>
</html>